CREATE  PROCEDURE DBProc_GetNextID
	@parent_name nvarchar(64),
        @rand 	int
AS
DECLARE @cur_id int,
        @str_sql nvarchar(200)

BEGIN TRANSACTION

DECLARE cursor1 CURSOR
 FOR SELECT next_id FROM tb_next_id
     WHERE obj_name=@parent_name
OPEN cursor1
FETCH NEXT FROM cursor1 INTO @cur_id

if (@@fetch_status<>-1)
  BEGIN
       select @cur_id = @cur_id + @rand
       UPDATE tb_next_id SET next_id=@cur_id       WHERE obj_name=@parent_name
  END
else
  BEGIN
       select @cur_id=@rand

       INSERT INTO tb_next_id (obj_name,next_id)  VALUES(@parent_name,@cur_id)
  END

CLOSE cursor1
DEALLOCATE   cursor1

COMMIT TRAN

SELECT @cur_id AS retval

GO